package com.example.chongwushangcheng.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.chongwushangcheng.model.Comment;
import com.example.chongwushangcheng.model.Reply;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 评论适配器
 */
public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentViewHolder> {

    private Context context;
    private List<Comment> commentList;
    private OnReplyClickListener replyClickListener;

    public CommentAdapter(Context context, List<Comment> commentList) {
        this.context = context;
        this.commentList = commentList;
    }

    @NonNull
    @Override
    public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_comment, parent, false);
        return new CommentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) {
        Comment comment = commentList.get(position);
        
        // 设置评论基本信息
        holder.ivAvatar.setImageResource(comment.getAvatarResId());
        holder.tvUsername.setText(comment.getUsername());
        holder.tvCommentContent.setText(comment.getContent());
        holder.tvCommentTime.setText(comment.getTime());
        
        // 设置作者标签
        if (comment.isAuthor()) {
            holder.tvUserTag.setVisibility(View.VISIBLE);
        } else {
            holder.tvUserTag.setVisibility(View.GONE);
        }
        
        // 清除回复容器中的所有视图
        holder.replyContainer.removeAllViews();
        
        // 添加回复
        if (comment.getReplies() != null && !comment.getReplies().isEmpty()) {
            holder.replyContainer.setVisibility(View.VISIBLE);
            
            for (Reply reply : comment.getReplies()) {
                View replyView = LayoutInflater.from(context).inflate(R.layout.item_comment, holder.replyContainer, false);
                
                CircleImageView replyAvatar = replyView.findViewById(R.id.iv_avatar);
                TextView replyUsername = replyView.findViewById(R.id.tv_username);
                TextView replyUserTag = replyView.findViewById(R.id.tv_user_tag);
                TextView replyContent = replyView.findViewById(R.id.tv_comment_content);
                TextView replyTime = replyView.findViewById(R.id.tv_comment_time);
                TextView replyBtn = replyView.findViewById(R.id.tv_reply);
                
                replyAvatar.setImageResource(reply.getAvatarResId());
                replyUsername.setText(reply.getUsername());
                replyContent.setText(reply.getContent());
                replyTime.setText(reply.getTime());
                
                if (reply.isAuthor()) {
                    replyUserTag.setVisibility(View.VISIBLE);
                } else {
                    replyUserTag.setVisibility(View.GONE);
                }
                
                // 隐藏回复按钮和回复容器
                replyBtn.setVisibility(View.GONE);
                replyView.findViewById(R.id.reply_container).setVisibility(View.GONE);
                
                holder.replyContainer.addView(replyView);
            }
        } else {
            holder.replyContainer.setVisibility(View.GONE);
        }
        
        // 设置回复点击监听器
        final int commentPosition = position;
        holder.tvReply.setOnClickListener(v -> {
            if (replyClickListener != null) {
                replyClickListener.onReplyClick(commentPosition, comment);
            }
        });
    }

    @Override
    public int getItemCount() {
        return commentList.size();
    }
    
    // 设置回复点击监听器
    public void setOnReplyClickListener(OnReplyClickListener listener) {
        this.replyClickListener = listener;
    }
    
    // 回复点击监听器接口
    public interface OnReplyClickListener {
        void onReplyClick(int position, Comment comment);
    }

    static class CommentViewHolder extends RecyclerView.ViewHolder {
        CircleImageView ivAvatar;
        TextView tvUsername;
        TextView tvUserTag;
        TextView tvCommentContent;
        TextView tvCommentTime;
        TextView tvReply;
        LinearLayout replyContainer;

        public CommentViewHolder(@NonNull View itemView) {
            super(itemView);
            ivAvatar = itemView.findViewById(R.id.iv_avatar);
            tvUsername = itemView.findViewById(R.id.tv_username);
            tvUserTag = itemView.findViewById(R.id.tv_user_tag);
            tvCommentContent = itemView.findViewById(R.id.tv_comment_content);
            tvCommentTime = itemView.findViewById(R.id.tv_comment_time);
            tvReply = itemView.findViewById(R.id.tv_reply);
            replyContainer = itemView.findViewById(R.id.reply_container);
        }
    }
} 